<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/12 0012
 * Time: 13:34
 */

namespace App\Socket\Models;


class Protocol
{
    private $code = 0;

    private $data = null;

    private $ack = 0;

    private $cid = '';

    private $success = 1;

    private $message;

    const SUCCESS = 1;

    const ERROR = 0;
    /**
     * 心跳包
     */
    const CODE_HEARTBEAT = 101;
    /**
     * 用户登录
     */
    const CODE_LOGIN = 201;
    /**
     * 用户登出
     */
    const CODE_LOGOUT = 202;
    /**
     *
     */
    const CODE_RACE_RESULT = 301;

    public function __construct()
    {
        $this->cid = md5(microtime());
    }

    /**
     * @return int
     */
    public function getCode(): int
    {
        return $this->code;
    }

    /**
     * @param int $code
     */
    public function setCode(int $code)
    {
        $this->code = $code;
    }

    /**
     * @return null
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param null $data
     */
    public function setData($data)
    {
        $this->data = $data;
    }

    /**
     * @return int
     */
    public function getAck(): int
    {
        return $this->ack;
    }

    /**
     * @param int $ack
     */
    public function setAck(int $ack)
    {
        $this->ack = $ack;
    }

    /**
     * @return string
     */
    public function getCid(): string
    {
        return $this->cid;
    }

    /**
     * @param string $cid
     */
    public function setCid(string $cid)
    {
        $this->cid = $cid;
    }

    /**
     * @return int
     */
    public function getSuccess(): int
    {
        return $this->success;
    }

    /**
     * @param int $success
     */
    public function setSuccess(int $success)
    {
        $this->success = $success;
    }

    /**
     * @return mixed
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param mixed $message
     */
    public function setMessage($message)
    {
        $this->message = $message;
    }


    /**
     * 转成数组格式
     * @return array
     */
    public function toArray()
    {
        return [
            'code' => $this->code,
            'data' => $this->data,
            'ack' => $this->ack,
            'cid' => $this->cid,
            'success' => $this->success,
            'message' => $this->message
        ];
    }

    /**
     * 生成cid
     * @return string
     */
    public static function makeCid()
    {
        return md5(microtime());
    }

    /**
     * @return string
     */
    public function toJson()
    {
        return json_encode($this->toArray());
    }

    /**
     * 数组转为对象
     * @param $data
     * @return Protocol
     */
    public static function toObject($data)
    {
        $protocol = new Protocol();
        $protocol->setCode($data['code'] ?? 0);
        $protocol->setAck($data['ack'] ?? 0);
        $protocol->setCid($data['cid'] ?? $protocol->makeCid());
        $protocol->setData($data['data'] ?? null);
        $protocol->setSuccess($data['success'] ?? 1);
        $protocol->setMessage($data['message'] ?? '');
        return $protocol;
    }
}